package de.jmda.util.security;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

import de.jmda.gui.swing.mvc.ControllerContextProvider;
import de.jmda.gui.swing.mvc.ControllerSupportEnabled;

/**
 *
 */
@ControllerSupportEnabled
public class DialogLoginRegisterRemind
    extends javax.swing.JDialog
		implements ControllerContextProvider
{
    public enum ExitCode
    {
      LOGIN,
      REGISTER,
      REMIND,
      CANCEL
    }

//  	@de.jmda.gui.swing.mvc.ControllerSupportDisabled
//    private final static Logger LOGGER =
//    		Logger.getLogger(DialogLoginRegisterRemind.class);
    
    private ExitCode exitCode = ExitCode.CANCEL;

    /** Creates new form DialogLoginRegisterRemind */
    public DialogLoginRegisterRemind(java.awt.Frame parent, boolean modal)
    {
      super(parent, "login or register", modal);

      try
      {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      }
      catch (Exception e)
      {
        // no exception expected with system look and feel
//        LOGGER.error("failure setting system look and feel\n" + e);
      }
      
      initComponents();
    }

    public ExitCode getExitCode()
    {
      return exitCode;
    }

    public String getUsername()
    {
      return jTextFieldUsername.getText();
    }

    public char[] getPassword()
    {
      return jPasswordField.getPassword();
    }

    void setExitCode(ExitCode exitCode)
    {
    	this.exitCode = exitCode;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {
    GridBagConstraints gridBagConstraints;

    jLabelUsername = new JLabel();
    jLabelPassword = new JLabel();
    jTextFieldUsername = new JTextField();
    jPasswordField = new JPasswordField();
    jPanelButtons = new JPanel();
    jButtonLogin = new JButton();
    jButtonRegister = new JButton();
    jButtonRemind = new JButton();
    jButtonCancel = new JButton();

    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    getContentPane().setLayout(new GridBagLayout());

    jLabelUsername.setText("username");
    getContentPane().add(jLabelUsername, new GridBagConstraints());

    jLabelPassword.setText("password");
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    getContentPane().add(jLabelPassword, gridBagConstraints);
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    getContentPane().add(jTextFieldUsername, gridBagConstraints);
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    getContentPane().add(jPasswordField, gridBagConstraints);

    jPanelButtons.setLayout(new GridBagLayout());

    jButtonLogin.setText("login");
    jButtonLogin.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        jButtonLoginActionPerformed(evt);
      }
    });
    jPanelButtons.add(jButtonLogin, new GridBagConstraints());

    jButtonRegister.setText("register");
    jButtonRegister.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        jButtonRegisterActionPerformed(evt);
      }
    });
    jPanelButtons.add(jButtonRegister, new GridBagConstraints());

    jButtonRemind.setText("remind");
    jButtonRemind.setToolTipText("send reminder about username and password to eMail address");
    jPanelButtons.add(jButtonRemind, new GridBagConstraints());

    jButtonCancel.setText("cancel");
    jButtonCancel.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        jButtonCancelActionPerformed(evt);
      }
    });
    jPanelButtons.add(jButtonCancel, new GridBagConstraints());

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.gridwidth = 2;
    getContentPane().add(jPanelButtons, gridBagConstraints);

    pack();
  }// </editor-fold>//GEN-END:initComponents

    private void jButtonLoginActionPerformed(ActionEvent evt)//GEN-FIRST:event_jButtonLoginActionPerformed
    {//GEN-HEADEREND:event_jButtonLoginActionPerformed
      controller.login();
    }//GEN-LAST:event_jButtonLoginActionPerformed

    private void jButtonRegisterActionPerformed(ActionEvent evt)//GEN-FIRST:event_jButtonRegisterActionPerformed
    {//GEN-HEADEREND:event_jButtonRegisterActionPerformed
      controller.register();
    }//GEN-LAST:event_jButtonRegisterActionPerformed

    private void jButtonCancelActionPerformed(ActionEvent evt)//GEN-FIRST:event_jButtonCancelActionPerformed
    {//GEN-HEADEREND:event_jButtonCancelActionPerformed
      controller.cancel();
    }//GEN-LAST:event_jButtonCancelActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DialogLoginRegisterRemind dialog =
                		new DialogLoginRegisterRemind(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private JButton jButtonCancel;
  private JButton jButtonLogin;
  private JButton jButtonRegister;
  private JButton jButtonRemind;
  private JLabel jLabelPassword;
  private JLabel jLabelUsername;
  private JPanel jPanelButtons;
  private JPasswordField jPasswordField;
  private JTextField jTextFieldUsername;
  // End of variables declaration//GEN-END:variables

	//GEN-BEGIN:controller support
	@de.jmda.gui.swing.mvc.ControllerSupportDisabled                     
	private de.jmda.util.security.DialogLoginRegisterRemindController controller =                       
			new de.jmda.util.security.DialogLoginRegisterRemindController(this);                             
                                                                         
	public de.jmda.gui.swing.mvc.ControllerContext getControllerContext()
	{                                                                      
		return new de.jmda.util.security.DialogLoginRegisterRemindControllerContext(
				this,
				exitCode,
				jButtonCancel,
				jButtonLogin,
				jButtonRegister,
				jButtonRemind,
				jLabelPassword,
				jLabelUsername,
				jPanelButtons,
				jPasswordField,
				jTextFieldUsername);                             
	}                                                                      
	//GEN-END:controller support
}